package cn.yuanqiao.archive.webservice.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/29 17:27
 */

import cn.yuanqiao.archive.webservice.service.CqbcWebservice;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.cqbc.dataManage.domain.DataManage;
import cn.yuanqiao.cqbc.dataManage.mapper.DataManageMapper;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/29 17:27
 *@Description:
 *@Note:
 */
@Service
@WebService(serviceName  = "CqbcSaveArchive",    // 与接口中的name相同
        targetNamespace = "http://service.webservice.archive.yuanqiao.cn",  // 一般为当前包名的倒序
        endpointInterface = "cn.yuanqiao.archive.webservice.service.CqbcWebservice"  // 为接口类的包名
)
public class CqbcWebserviceImpl implements CqbcWebservice {
    private final Logger logger = LoggerFactory.getLogger(CqbcWebserviceImpl.class);

    @Autowired
    private DataManageMapper dataManageMapper;
    @Value("${HD_RECEIVE_PATH}")
    private String hdReceivePath;
    @Override
    public String CqbcSaveArchive(String paketInfo,String file2base64) throws IOException {
        logger.info("paketInfo={}",paketInfo);
        logger.info("file2base64={}",file2base64);
        JSONObject jsonObject = JSONObject.parseObject(paketInfo);
        String name=jsonObject.getString("name");
        String size=jsonObject.getString("size");
        String format=jsonObject.getString("format");
        String fonds=jsonObject.getString("fonds");
        String category=jsonObject.getString("category");
        String dh=jsonObject.getString("dh");
        String title=jsonObject.getString("title");
        String tenantCode=jsonObject.getString("tenantCode");
        String biaoshi=jsonObject.getString("biaoshi");
        String zlfs=jsonObject.getString("zlfs");
        String saveTime=jsonObject.getString("saveTime");
        JSONObject json = new JSONObject(1);
        try {
            //判断标识,已经推送过的直接覆盖数据
            Long hadid = dataManageMapper.finddatabybiaoshi(biaoshi);
            if (null != hadid && !"".equals(hadid)) {
                //存在时直接删除
                //删除主表数据,删除YQ_DATA_MANAGE数据
                dataManageMapper.deletedata(hadid, tenantCode);
            }
            byte[] data = java.util.Base64.getDecoder().decode(file2base64);
            FileUtils.writeByteArrayToFile(new File(hdReceivePath + name + "." + format), data);
            DataManage dataManage = new DataManage();
            dataManage.setName(name);
            dataManage.setId(SnowflakeIdGenerator.generateId());
            dataManage.setSize(size);
            dataManage.setFormat(format);
            dataManage.setFonds(fonds);
            dataManage.setPath(hdReceivePath + name + "." + format);
            dataManage.setCategory(Long.parseLong(category));
            dataManage.setDh(dh);
            dataManage.setIsdel("0");
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dataManage.setReceivingTime(sdf.format(date));
            dataManage.setTenantCode(tenantCode);
            dataManage.setTitle(title);
            dataManage.setZlfs(zlfs);
            dataManage.setSaveTime(saveTime);
            dataManage.setBiaoshi(Long.parseLong(biaoshi));
            int i = dataManageMapper.insert(dataManage);
            json.put("msg","success" );
            return json.toString();
        }catch (Exception e){
            json.put("msg","fail" );
            e.printStackTrace();
            return json.toString();
        }
    }

}
